const {
  add,
  del,
  edit,
  search,
  searchBy,
} = require('../js/mysql.js')
const router = require('koa-router')()
const { dateFormat } = require('@jiess/utils')
const jwtAuth = require('../jwtAuth')

router.use(jwtAuth)

router.prefix('/train')

router.get('/proget', async (ctx, next) => {
  const { date } = ctx.request.query;
  let data = await searchBy(`pro_date=${date}`, 'progress');
  ctx.body = {
    code: 200,
    msg: '操作成功',
    data
  }
})
router.post('/proadd', async (ctx, next) => {
  const {date} = ctx.request.body;
  date = dateFormat(date, 'yyyy-MM-dd');
  await add([pro_date, quantity], `progress (pro_date,quantity) values ('${date}',1)`)
  ctx.body = {
    code: 200,
    msg: "操作成功"
  }
})
router.put('/proedit/:id', async (ctx, next) => {
  const { id } = ctx.params
  const { quantity } = ctx.request.body;
  await edit([quantity], `quantity=${quantity} where id=${id}`, 'progress')
})
// 添加培训 
router.post('/add', async (ctx, next) => {
  const { poster, train_name, train_category, train_credits, organizer, start_time, end_time, begin, Introduction, sign_number, account, phone, train_state } = ctx.request.body;
  let param = [poster, train_name, train_category, train_credits, organizer, start_time, end_time, begin, Introduction, sign_number, account, phone, train_state];
  let sql = `train (poster,train_name,train_category,train_credits,organizer,start_time,end_time,begin,Introduction,sign_number,train_admin,admin_phone,train_state)
  values ('${poster}','${train_name}','${train_category}','${train_credits}','${organizer}','${start_time}','${end_time}','${begin}','${Introduction}','${sign_number}','${account}','${phone}',${train_state})`
  await add(param, sql);
  ctx.body = {
    code: 200,
    msg: "操作成功"
  }
})
// 完整查询
router.get('/getall',async(ctx,next) => {
  let data = await search('train')
  ctx.body = {
    code: 200,
    msg: "操作成功",
    data
  }
})
// 分页/搜索列表展示
router.get('/pagination', async (ctx, next) => {
  const { page = 1, limit = 3, search = "" } = ctx.request.query;
  let sql1 = `user_state=1 and train_state!=6 and (train_name LIKE '%${search}%' or train_admin LIKE '%${search}%')`;
  let sql2 = `user_state=1 and train_state!=6 and (train_name LIKE '%${search}%' or train_admin LIKE '%${search}%') order by id DESC LIMIT ${(page - 1) * limit},${limit}`;
  let total = await searchBy(sql1, 'train');
  let data = await searchBy(sql2, 'train');
  let format = 'yyyy-MM-dd HH:mm:ss';
  ctx.body = {
    code: 200,
    msg: "操作成功",
    data:data.map(item => {
      item.begin = dateFormat(item.begin, format)
      item.start_time = dateFormat(item.start_time, format)
      item.end_time = dateFormat(item.end_time, format)
      return item;
    }),
    total: total.length
  }
})
// 根据id查询培训信息
router.get('/trainInfo/:id', async (ctx, next) => {
  const { id } = ctx.params;
  let data = await searchBy(`id=${id}`, 'train');
  let format = 'yyyy-MM-dd HH:mm:ss';
  ctx.body = {
    code: 200,
    msg: '操作成功',
    data: data.map(item => {
      item.begin = dateFormat(item.begin, format)
      item.start_time = dateFormat(item.start_time, format)
      item.end_time = dateFormat(item.end_time, format)
      return item;
    })
  }
})
// 修改培训信息
router.put('/change/:id', async (ctx, next) => {
  const { id } = ctx.params;
  const { poster, train_name, train_category, train_credits, organizer, start_time, end_time, begin, Introduction, sign_number } = ctx.request.body;
  let param = [poster, train_name, train_category, train_credits, organizer, start_time, end_time, begin, Introduction, sign_number];
  let sql = `poster='${poster}',train_name='${train_name}',train_category='${train_category}',train_credits='${train_credits}',organizer='${organizer}',start_time='${start_time}',end_time='${end_time}',begin='${begin}',Introduction='${Introduction}',sign_number='${sign_number}' where id=${id}`;
  await edit(param, sql, 'train')
  ctx.body = {
    code: 200,
    msg: '操作成功'
  }
})
// 修改状态
router.put('/changestate/:id', async (ctx, next) => {
  const { id } = ctx.params;
  const { train_state } = ctx.request.body;
  await edit([train_state], `train_state=${train_state} where id=${id}`, 'train');
  ctx.body = {
    code: 200,
    msg: '操作成功'
  }
})
// 审核表新增
router.post('/audit', async (ctx, next) => {
  const { train_id, reviewer, reason } = ctx.request.body;
  let sql = `auditform (train_id,reviewer,reason) values (${train_id},'${reviewer}','${reason}')`
  await add([train_id, reviewer, reason], sql);
  ctx.body = {
    code: 200,
    msg: '操作成功'
  }
})
// 删除（假）
router.put('/delf', async (ctx, next) => {
  const IDlist = ctx.request.body;
  for (let i in IDlist) {
    await edit([6], `train_state=6 where id=${IDlist[i]}`, 'train')
  };
  ctx.body = {
    code: 200,
    msg: '操作成功'
  }
})

module.exports = router